home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / util / cli / iuw_clitools.lha / src / match.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  3KB  |  98 lines

  1. /* match - check if string matches pattern
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  * V1.0: 27/Nov/94 first version (@ Bielefeld Meeting '94)
  13.  * V1.1: 05/Dec/94 code cleanup, bugfix
  14.  * V1.2: 01/Jul/95 replaced a few error messages with a PrintFault() call
  15.  */
  16. #define THIS_PROGRAM    "match"
  17. #define THIS_VERSION    "1.2"
  18.  
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <dos/dos.h>
  22. #include <dos/rdargs.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #define SysBase_DECLARED
  27. #include <proto/exec.h>
  28. #include <proto/dos.h>
  29. #include <proto/utility.h>
  30. #define PatternBase_DECLARED
  31. #include <proto/pattern.h>
  32.  
  33. extern struct Library *SysBase;
  34. struct Library *PatternBase = NULL;
  35.  
  36. static const char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";
  37.  
  38. const char Template[] = "STRING/A,PAT=PATTERN/A,NOCASE/S";
  39. __aligned struct {
  40.     STRPTR  string;
  41.     STRPTR  pattern;
  42.     LONG    nocase;
  43. } Args;
  44.  
  45. void
  46. _main()
  47. {
  48.     struct RDArgs *rdargs;
  49.     LONG err, rc = RETURN_OK;
  50.  
  51.     if( SysBase->lib_Version < 37 ) {
  52.         #define MESSAGE "requires AmigaOS 2.04 or higher\n"
  53.         Write(Output(), MESSAGE, sizeof(MESSAGE)-1);
  54.         _exit(RETURN_FAIL);
  55.         #undef MESSAGE
  56.     }
  57.  
  58.     if( PatternBase = OpenLibrary(PATLIB_NAME, PATLIB_MIN_VERSION) ) {
  59.         if( rdargs = ReadArgs(Template, (LONG *)&Args, NULL) ) {
  60.             if( Args.nocase )
  61.                 err = SimpleMatchNoCase(Args.pattern, Args.string);
  62.             else
  63.                 err = SimpleMatch(Args.pattern, Args.string);
  64.  
  65.             switch( err ) {
  66.                 case 0:
  67.                     rc = RETURN_WARN;
  68.                     break;
  69.                 case 1:
  70.                     rc = RETURN_OK;
  71.                     err = 0;
  72.                     break;
  73.                 default:
  74.                     rc = RETURN_FAIL;
  75.                     err = PatternError2DOS(err);
  76.             }
  77.             FreeArgs(rdargs);
  78.         }
  79.         else {
  80.             err = IoErr();
  81.             rc = RETURN_ERROR;
  82.         }
  83.         CloseLibrary(PatternBase);
  84.     }
  85.     else {
  86.         /* no pattern library */
  87.         PutStr(THIS_PROGRAM ": " PATLIB_NAME " not found\n");
  88.         err = 0;
  89.         rc = RETURN_FAIL;
  90.     }
  91.  
  92.     if( err )
  93.         PrintFault(err, THIS_PROGRAM);
  94.  
  95.     _exit((int)rc);
  96. }
  97.  
  98.